<?php
/**
 * 农行信用卡1磅升2磅活动
 * User: guojun.song
 * Date: 15-06-11
 */
quanli_require('module/base/activity/activityConfig.class.php');

class activityNh {

    /**
     * 得到活动的信息
     * @return $activity_array
     * @author  20141219 guojun.song
     */
    public function activity_master(&$arg_array){
        global $G_SHOP;

        //实例化配置类
        $activity_config = new activityConfig('activity_nh2015'); //农行五折购活动配置
        $config_array = $activity_config->config;

        $isbool=false;
        //sql字符串
        $sql = ' ';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        //会员名
        if (isset($arg_array['custID']) && is_numeric($arg_array['custID'])) {
            $sql .= ' AND custID = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['custID']);
            $isbool=true;
        }

        //会员ID
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['pcustID'];
            $isbool=true;
        }
        //促销渠道ID
        if (isset($config_array['billPromotion']) && !empty($config_array['billPromotion'])) {
            $sql .= ' AND billPromotion=?';
            $params_type .= 's';
            $params_array[] = $config_array['billPromotion'];
        }

        //得到农行一磅两磅五折购使用状态 0：未使用，1：已使用
        if($isbool){
            $sql_master="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND
                         createTime>'".date('Y-m-d 00:00:00')."' AND createTime<'".date('Y-m-d 23:59:59')."' ".$sql;
            $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master,$params_type,$params_array);
            $item_count = $temp_item_master[0]->item_count;
            if($item_count>0){ //已使用农行信用卡1磅升2磅五折购
                $status = 1;
            }else{
                $status = 0;
            }
        }

        if(date('Y-m-d H:i:s') < '2015-07-01 00:00:00' ){
            $sql_master2 ="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND
               createTime>'2015-06-01 00:00:00' AND createTime<'2015-12-31 23:59:59' AND billPromotion=".$config_array['billPromotion'];
            $config_array['cake_sum'] = 2000;
        }else{
            //得到活动的每日蛋糕数量
           // $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND
      //  createTime>'".date('Y-m-d 00:00:00')."' AND createTime<'".date('Y-m-d 23:59:59')."' AND billPromotion=".$config_array['billPromotion'];

        }


        $temp_item_master2 = &$G_SHOP->DBCA->stmt_query($sql_master2);

        $number=$config_array['cake_sum'];

        if($temp_item_master2[0]->item_count){
            $number = $number - ($temp_item_master2[0]->item_count);
        }

        if($number<0) $number=0;

        //是否在活动时间内 0：未在活动时间内；1：在活动时间内
        $isDate=0;
        $Message="";
        $title="";
        $haveActivity = true;

        //判断今天是否在已排除的周期活动内
        if(isset($config_array['exclude_week']) && !empty($config_array['exclude_week'])){
            $w = date('w');
            if(is_array(json_decode($config_array['exclude_week'])) && in_array($w,json_decode($config_array['exclude_week']))){
                $haveActivity = false;//不在活动范围内
            }else{
                $isDate = 1;//在活动范围内
            }

        }
        if(!$isDate){
            $week = array();
            $isContinuous = 1;

            $arr = array(0,1,2,3,4,5,6);
            $json_arr = json_decode($config_array['exclude_week']);
            $diff_arr = array_diff($arr,$json_arr);

            sort($diff_arr,SORT_NUMERIC);

            if($diff_arr[0]+1 != $diff_arr[1]){
                $isContinuous = 0;//不是连续的时间段 而是离散的时间点
            }

            for($i=1;$i<=7;$i++){
                $nextWeek = date('w',strtotime("+".$i." day"));
                $nextDay  = date('Y-n-d',strtotime("+".$i." day"));

                if(!in_array($nextWeek,$json_arr)){
                    array_push($week,$nextDay);
                }
            }

            if(is_array($week) && count($week)>=2){
                $start = array_shift($week);
                $end   = array_pop($week);
            }

            if($isContinuous){
                list($year,$month,$day) = explode('-',$start);
                list($year_end,$month_end,$day_end) = explode('-',$end);
                $Message['start'] = $month.'月'.$day.'日';
                $Message['end']   = $month_end.'月'.$day_end.'日';
                $showtime = 0;
            }else{
                $Message ='订购时间为每周三和每周六两天';
                $showtime = 1;
            }

        }

        //用户信息数组
        $activity_array = array(
            'billPromotion' => $config_array['billPromotion'],//促销渠道ID
            'activity_status'	     => $status,//使用状态
            'endtime'	     => $config_array['endTime'],//活动结束时间
            'activity_num' => $number,//每日蛋糕所剩数量
            'isDate'        => $isDate,//是否在活动时间内
            'send_end_time' => $config_array["send_end_time"], //配送结束时间
            'activity_date' => $config_array["activity_date"], //活动的订购日期
            'paytypeID' => $config_array["paytypeID"], //在线银行浦发银行paytypeID
            'message' => $Message, //下次活动提示时间
            'showtime'=> $showtime,
            'titleDate' => $title //每次显示的订购时间
        );

        return $activity_array;

    }


    /**
     * 记录页面上按钮的点击次数
     */
    public function pf_yhk_log($sku){
        global $G_SHOP;

        try {
            $G_SHOP->DBCA->beginTransaction();

            $activityLog = new entity('activity_log');
            $activityLog->creator = $G_SHOP->currentCustomerID==""?$this->getIP():$G_SHOP->currentCustomerID;
            $activityLog->pcustID = $G_SHOP->currentPCustID;
            $activityLog->sku = $sku;

            $G_SHOP->DBCA->save($activityLog) ;
            $G_SHOP->DBCA->commit();
            return array( 'code' => 200, 'message' => '记录成功' );
        } catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }
    }

    /**
     * 查询IP
     * @return $realip
     * @author  20141219 allen.qiang <allen.qiang@fistforward.com>
     */
    public function getIP()
    {
        static $realip;
        if (isset($_SERVER)){
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")){
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
        return $realip;
    }


} 